//
//  ImageShowCell.swift
//  Employee
//
//  Created by swordjoy on 2017/3/28.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class ImageShowCell: UITableViewCell {
  static var identifier = "ImageShowCell.swordjoy"
  static var height: CGFloat = 150
  
  var models: [String] = [] {
    didSet {
      if models.isEmpty {
        addLabel.isHidden = true
        collectionView.isHidden = true
        return
      }
      else {
        addLabel.isHidden = false
        collectionView.isHidden = false
      }
      collectionView.reloadData()
    }
  }
    var titleName: String = "" {
        didSet {
            addLabel.text = titleName.isEmpty ? "附件(选填)" : titleName
        }
    }
  
  var cellDidClick: ( (Int, UICollectionView) -> () )?
  
  fileprivate var addLabel: UILabel!
  fileprivate var collectionView: UICollectionView!
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    backgroundColor = UIColor.white
    selectionStyle = .none
    create()
  }
  
  override func layoutSubviews() {
    super.layoutSubviews()
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
}

// MARK: - fileprivate

extension ImageShowCell {
  fileprivate func create() {
    addLabel = UILabel()
    addSubview(addLabel)
    addLabel.font = GlobalConfig.system12Font
    addLabel.textColor = GlobalConfig.TextLightColor
    addLabel.text = "附件(选填)"
    addLabel.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.top.equalToSuperview()
    }
    
    let flow = UICollectionViewFlowLayout()
    let width = (UIScreen.width - 80) / 4
    flow.itemSize = CGSize(width: width, height: width)
    flow.minimumInteritemSpacing = 10
    
    collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flow)
    collectionView.backgroundColor = UIColor.clear
    collectionView.contentInset = UIEdgeInsets(top: 15, left: 20, bottom: 0, right: 20)
    addSubview(collectionView)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.register(ImageSelectCell.self, forCellWithReuseIdentifier: ImageSelectCell.identifier)
    collectionView.register(ChooseImageCell.self, forCellWithReuseIdentifier: ChooseImageCell.identifier)
    collectionView.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.top.equalTo(addLabel.snp.bottom)
      make.bottom.equalTo(-10)
    }
  }
}

extension ImageShowCell: UICollectionViewDataSource {
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return models.count
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageSelectCell.identifier, for: indexPath) as! ImageSelectCell
    cell.imageName = models[indexPath.row]
    cell.isHideDeleteButton = true
    
    return cell
    
  }
}

extension ImageShowCell: UICollectionViewDelegate {
  
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    var imageViews: [UIImageView] = []
    let cells = collectionView.visibleCells
    for cell in cells {
        let temp = cell as! ImageSelectCell
        imageViews.append(temp.imageV)
    }
    
    cellDidClick?(indexPath.row, collectionView)
  }
  
}

